package cn.iocoder.yudao.module.other.job;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
import cn.iocoder.yudao.module.other.controller.admin.weibo.vo.WeiboHotSearchSaveReqVO;
import cn.iocoder.yudao.module.other.job.vo.HotSearchRespDto;
import cn.iocoder.yudao.module.other.service.weibo.WeiboHotSearchService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class WeiboHotJob implements JobHandler {


    private final String HOT_SEARCH_URL = "https://weibo.com/ajax/side/hotSearch";

    @Resource
    private WeiboHotSearchService weiboHotSearchService;

    @Override
    @TenantIgnore
    public String execute(String param) {

        log.info("微博热搜拉取开始");
        Map<String, String> header = getHeader();

        String hotSearchRespStr = HttpRequest.get(HOT_SEARCH_URL).addHeaders(header).execute().body();

        log.info("微博热搜拉取到数据:{}",hotSearchRespStr);

        HotSearchRespDto hotSearchRespDto = JSON.parseObject(hotSearchRespStr, HotSearchRespDto.class);
        if (hotSearchRespDto.getOk() == 1) {

            Date date = new Date();
            String logTimeStr = DateUtil.format(date, "yyyyMMddHH");
            Long logTime = Long.valueOf(logTimeStr);
            List<HotSearchRespDto.DataDTO.HotgovsDTO> hotgovs = hotSearchRespDto.getData().getHotgovs();
            if (!CollectionUtils.isEmpty(hotgovs)) {
                for (HotSearchRespDto.DataDTO.HotgovsDTO hotgovsDTO : hotgovs) {
                    WeiboHotSearchSaveReqVO weiboHotSearchSaveReqVO = new WeiboHotSearchSaveReqVO();
                    weiboHotSearchSaveReqVO.setWord(hotgovsDTO.getName());
                    weiboHotSearchSaveReqVO.setWordScheme(hotgovsDTO.getName());
                    weiboHotSearchSaveReqVO.setRank(-1);
                    weiboHotSearchSaveReqVO.setLogTime(logTime);
                    weiboHotSearchService.createWeiboHotSearch(weiboHotSearchSaveReqVO);
                }
            }
            List<HotSearchRespDto.DataDTO.RealtimeDTO> hotList = hotSearchRespDto.getData().getRealtime();
            if (!CollectionUtils.isEmpty(hotList)) {
                for (HotSearchRespDto.DataDTO.RealtimeDTO realtimeDTO : hotList) {
                    WeiboHotSearchSaveReqVO weiboHotSearchSaveReqVO = new WeiboHotSearchSaveReqVO();
                    weiboHotSearchSaveReqVO.setWord(realtimeDTO.getWord());
                    weiboHotSearchSaveReqVO.setRank(realtimeDTO.getRank());
                    weiboHotSearchSaveReqVO.setNum(realtimeDTO.getNum());
                    weiboHotSearchSaveReqVO.setWordScheme(realtimeDTO.getWordScheme());
                    weiboHotSearchSaveReqVO.setLogTime(logTime);
                    weiboHotSearchService.createWeiboHotSearch(weiboHotSearchSaveReqVO);
                }
            }

        }

        return "success";
    }


    private Map<String, String> getHeader() {

        Map<String, String> header = new HashMap<>();
        header.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36");
        header.put("Accept", "application/json, text/plain, */*");

        return header;
    }

}
